package com.flyqiu.flow.core.fliter;

import lombok.Data;
import com.flyqiu.flow.bpmn.element.BaseProcElement;

import java.util.List;

@Data
public class DefaultBpmnPathFilter implements BpmnPathFilter {

    /**
     * 元素类型
     */
    private Class<?> bpmnElementType;
    /**
     * 最多的元素层级
     */
    private int maxElementLevel = 2;

    public void setBpmnElementType(Class<?> bpmnElementType) {
        this.bpmnElementType = bpmnElementType;
    }

    public void setMaxElementLevel(int maxElementLevel) {
        if (maxElementLevel < 1) {
            throw new RuntimeException("MaxElementLevel cannot be less than 1");
        }
        this.maxElementLevel = maxElementLevel;
    }

    @Override
    public boolean filter(String pathName, List<BaseProcElement> path) {
        if (null == bpmnElementType) {
            return maxElementLevel > path.size();
        }
        long count = path.stream().filter(e -> e.getClass().equals(bpmnElementType)).count();
        return this.maxElementLevel > count;
    }
}
